package org.mk.script.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Test {
	public static int lineno = 0;
	public static int tokenval = 0;
	public static String script;
	public static int curIndex = 0;
	public static Map lexMap = new HashMap();

	public static final int NONE = -1;
	public static final int EOS = '\0';
	public static final int NUM = 256;
	public static final int DIV = 257;
	public static final int MOD = 258;
	public static final int ID = 259;
	public static final int DONE = 260;

	public static int lastentry = 0;

	class Entry {
		String lexptr;
		int token;
	}

	public static Entry[] symtable = new Entry[100];

	public static void main(String args[]) {

		Test aTest = new Test();
		script = aTest.getScript();
		aTest.getToken();
	}

	public int lookup(String str) {
		int result = 0;
		for (int i = lastentry; i > 0; i--) {
			String pv = symtable[i].lexptr;
			if (str.equals(pv)) {
				return i + 1;
			}
		}
		return result;
	}

	public int insert(String str, int tok) {
		lastentry++;
		Entry entry = new Entry();
		entry.lexptr = str;
		entry.token = tok;
		symtable[lastentry] = entry;
		return lastentry;
	}

	public String getScript() {
		StringBuffer sbf = new StringBuffer();
		try {
			BufferedReader bfr = new BufferedReader(new FileReader(new File(
					"d:\\x.txt")));
			String line = bfr.readLine();
			while (line != null) {
				sbf.append(line);
				line = bfr.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sbf.toString();
	}

	public char getChar() {
		return script.charAt(curIndex++);
	}

	/**
	 * 读取标识符
	 */
	public int getToken() {
		while (true) {
			char t = getChar();
			if (curIndex > script.length() - 1) {
				return 0;
			}
			if (t == ' ' || t == '\t') {

			} else if (t == '\n') {
				lineno++;
			} else if (isletter(t)) {
				StringBuffer str = new StringBuffer();
				str.append(t);
				t = getChar();
				while (isletter(t) || isdigit(t)) {
					str.append(t);
					t = getChar();
				}
				ungetc();
				// 从字符表中查找
				int pIndex = lookup(str.toString());
				if (pIndex == 0) {
					insert(str.toString(), ID);
				}
			} else {

			}

		}
	}

	public void ungetc() {
		curIndex--;
	}

	public int lexan() {
		while (true) {
			char t = getChar();
			if (curIndex > script.length() - 1) {
				return 0;
			}
			if (t == ' ' || t == '\t') {

			} else if (t == '\n') {
				lineno++;
			} else if (isdigit(t)) {
				tokenval = t - '0';
				t = getChar();
				while (isdigit(t)) {
					tokenval = tokenval * 10 + t - '0';
					t = getChar();
				}

				ungetc();
				return tokenval;
			} else {
				tokenval = 0;
				// return t;
			}

		}
	}

	public boolean isdigit(char t) {
		if (t >= '0' && t <= '9') {
			return true;
		} else {
			return false;
		}
	}

	public boolean isletter(char t) {
		if ((t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z')) {
			return true;
		} else {
			return false;
		}
	}

}
